package mx.com.matersys.vacs.web;

import java.util.List;

import mx.com.matersys.vacs.domain.GenericResponse;
import mx.com.matersys.vacs.domain.Planservicio;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.roo.addon.web.mvc.controller.RooWebScaffold;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import flexjson.JSONSerializer;

@Secured("ROLE_ADMIN")
@RooWebScaffold(path = "planservicios", formBackingObject = Planservicio.class)
@RequestMapping("/planservicios")
@Controller
public class PlanservicioController {
	
	@RequestMapping(value = "/getListPlanes", method = RequestMethod.GET, headers = "Accept=application/json")
	@ResponseBody
	public ResponseEntity<String> getListPlanes() {
		
		List<Planservicio> listP;
		
		HttpHeaders headers = new HttpHeaders();
		headers.set("Content-Type", "application/json");
		headers.add("Content-Type", "application/text; charset=utf-8");
		
		listP = Planservicio.findAllPlanservicios();
		
		String json = new JSONSerializer().exclude("*.class").serialize(listP);
		return new ResponseEntity<String>(json, headers, HttpStatus.OK);
	}
	
	
	@RequestMapping(value = "/addPlanServicio", method = RequestMethod.POST)
	public ResponseEntity<String> addPlanServicio(Planservicio plan) {
		
		HttpHeaders headers;
		String json;
		GenericResponse gRes;
		ResponseEntity<String> response;
		
		headers = new HttpHeaders();
		headers.set("Content-Type", "application/json");
		headers.add("Content-Type", "application/text; charset=utf-8");
		
		System.out.println(plan);
		
		plan.persist();
		
		gRes = new GenericResponse(Boolean.TRUE, plan);
		json = new JSONSerializer().exclude("*.class").serialize(gRes);
		
		response = new ResponseEntity<String>(json, headers, HttpStatus.OK);
		
		return response; 
	}
	
	@RequestMapping(value = "/readPlan", method = RequestMethod.GET)
	public ResponseEntity<String> readPlanServicio(
			@RequestParam(value = "id", required = false) String idPlanservicio) {
		
		HttpHeaders headers;
		String json;
		GenericResponse gRes;
		ResponseEntity<String> response;
		Planservicio plan;
		
		headers = new HttpHeaders();
		headers.set("Content-Type", "application/json");
		headers.add("Content-Type", "application/text; charset=utf-8");
		
		plan = Planservicio.findPlanservicio(3);
		
		gRes = new GenericResponse(Boolean.TRUE, plan);
		json = new JSONSerializer().exclude("*.class").serialize(gRes);
		
		response = new ResponseEntity<String>(json, headers, HttpStatus.OK);
		
		return response; 
	}
	
	@RequestMapping(value = "/deletePlan", method = RequestMethod.POST)
	public ResponseEntity<String> deletePlanServicio(@RequestParam(value = "idPlanServicio") Integer idPlanservicio) {
		
		HttpHeaders headers;
		String json;
		GenericResponse gRes;
		ResponseEntity<String> response;
		
		headers = new HttpHeaders();
		headers.set("Content-Type", "application/json");
		headers.add("Content-Type", "application/text; charset=utf-8");
		
		//TODO JMMG falta agregar manejo de excepciones
		//Planservicio.findPlanservicio(idPlanservicio).remove();
		
		gRes = new GenericResponse(Boolean.TRUE, null);
		json = new JSONSerializer().exclude("*.class").serialize(gRes);
		
		response = new ResponseEntity<String>(json, headers, HttpStatus.OK);
		
		return response; 
	}

	/*
	@RequestMapping(value = "/eliminar/*", method = RequestMethod.DELETE)
	public @ResponseBody
			Map<String, Object> deleteAntecedente(@RequestBody Antecedente antecedente) {
		log.debug("deleteAntecedente");
		try {
			this.antecedenteServicio.delete(antecedente);
			
			return this.getExtSuccess(antecedente, systemMessages[2]);
		} catch (DataAccessException  e) {
			log.error("deleteAntecedente failed", e);
			return this.getExtError(systemMessages[3]);
		}
	}*/
	
}
